﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using System.Linq;
using Telerik.Web.UI;
using ud_gSDMS.Code_Service;

namespace ud_gSDMS.project
{
    public partial class report_total: mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;

        void Page_Load(object sender, EventArgs e)
        {
            if (SiteUtils.GetCurrentSiteUser()==null)
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    PopulateLabels();
                    PopulateControls();
                }
            }
        }
        /// <summary>
        /// Xuất dữ liệu từ GridView ra Excel
        /// </summary>
        private void XuatDuLieuRaXMLExcel()
        {
            //Tao report tu DataTable chay ok nhung ko hoan hao theo y nguoi dung
            DataTable tb = data;
            data.TableName = "TimeSheet";
            data.WriteXml(Server.MapPath(".") + @"\ExportTimeSheet.xls", XmlWriteMode.IgnoreSchema);
            Response.Redirect("ExportTimeSheet.xls");
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            //XuatDuLieuRaXMLExcel();
            var db = ViewState["listProjectReportTotal"];
            if (db != null)
            {
                hiddenGrid.DataSource = db;
                hiddenGrid.DataBind();
                var name = ("Project-Report-" + DateTime.Now.ToShortDateString().Replace("/", "").Replace(":", "").Replace(" ", "_"));
                hiddenGrid.ExportSettings.FileName = name;
                hiddenGrid.ExportSettings.IgnorePaging = true;
                hiddenGrid.ExportSettings.OpenInNewWindow = true;
                hiddenGrid.MasterTableView.ExportToExcel();
            }
        }
        private void PopulateLabels()
        {

        }
        private void PopulateControls()
        {
            datTimeReport_Date_from.SelectedDate = DateTime.Now.AddMonths(-3);
            datTimeReport_Date_to.SelectedDate = DateTime.Now;
            datTimeReport_Date_from.MaxDate = DateTime.Now;
            datTimeReport_Date_to.MaxDate = DateTime.Now;
            LoadControl();
            LoadGrid();

        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            //Xác nhận điều khiển HtmlForm tại thời gian chạy ASP.NET
        }

        private static DataTable data;
        private void LoadGrid()
        {
            data = gSDMS_BL.ud_gSDMS_Project_ReportTotal(datTimeReport_Date_from.SelectedDate == null ? null : datTimeReport_Date_from.SelectedDate.Value.ToShortDateString(),
                                                         datTimeReport_Date_to.SelectedDate == null ? null : datTimeReport_Date_to.SelectedDate.Value.ToShortDateString(),
                                                         cboProjectLeader.Text.Trim() == "" ? null : cboProjectLeader.SelectedValue,
                                                         cboProjectName.Text.Trim() == "" ? null : cboProjectName.SelectedValue);

            if (ViewState["SortExpression"] != null)
            {
                var dv = data.DefaultView;
                dv.Sort = ViewState["SortExpression"].ToString();
                data = dv.Table;
            }
            grdReportTotal.DataSource = data;
            grdReportTotal.DataBind();
            //for (int i = 0; i < grdReportTotal.Rows.Count; i++)
            //{
            //    if ((data.Rows[i]["Time_late"].ToString().Trim().Length) > 0)
            //    {
            //        grdReportTotal.Rows[i].FindControl("btnAccept").Visible = true;
            //    }
            //    else
            //    {
            //        // grdReport.Rows[i].FindControl("btnAccept").Visible = false;
            //    }
            //}
           ViewState["listProjectReportTotal"] = data;
        }

        private void LoadControl()
        {
            var db = new gSDMS_DataLinq();
            //cboProjectLeader.DataSource =gSDMS_BL.getAccountID(SiteUtils.GetCurrentSiteUser().UserId.ToString());
            //cboProjectLeader.DataSource = gSDMS_BL.mp_User_Search(null, null, null, null, null, null, "Engineering");
            //cboProjectLeader.DataValueField = "UserID";
            //cboProjectLeader.DataTextField = "LoginName";
            //cboProjectLeader.DataBind();
            cboProjectLeader.DataValueField = "UserID";
            cboProjectLeader.DataTextField = "LoginName";
            cboProjectLeader.DataSource = gSDMS_BL.GetMemberProjectLeader();
            cboProjectLeader.DataBind();
            
            cboProjectName.DataValueField = "Project_ID";
            cboProjectName.DataTextField = "Name";
            cboProjectName.DataSource = Library.LINQToDataTable(db, db.ud_gSDMS_PROJECTs);
            cboProjectName.DataBind();
        }

        private void grdReportTotal_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdReportTotal.PageIndex = e.NewPageIndex;
            LoadGrid();
        }
        private void grdReportTotal_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                gSDMS_BL.ud_gSDMS_Project_Plan_Del(e.CommandArgument.ToString(), true);
            }
        }
        void grdReportTotal_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LoadGrid();
        }
        void grdReportTotal_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Direction"] == null) ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " ASC") ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " DESC") ViewState["Direction"] = " ASC";
            grdReportTotal.EditIndex = -1;
            string strSortExp = e.SortExpression + ViewState["Direction"].ToString();
            ViewState["SortExpression"] = strSortExp;
            LoadGrid();
        }

        void btnReportSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            LoadGrid();
        }

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            grdReportTotal.PageIndexChanging += grdReportTotal_PageIndexChanging;
            grdReportTotal.RowCommand += grdReportTotal_RowCommand;
            grdReportTotal.RowDeleting += grdReportTotal_RowDeleting;
            grdReportTotal.Sorting += grdReportTotal_Sorting;
            btnExportExcel.Click += btnExportExcel_Click;
            //btnExportExcel.Click += new EventHandler(btnExportExcel_Click);
            btnReportSearch.Click += btnReportSearch_Click;
            //btnFinish.Click += btnFinish_Click;
            SuppressPageMenu();
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}